﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Rendering;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using GISSystem.Class;
using GISSystem.WindowsForm;
using GISSystem.TocMenu;

namespace GISSystem
{
    public partial class frmMain : Form
    {
        //主界面类，用于对界面的样式进行控制
        MainFormUI mainUI;

        //地图控制类
        public MapOperator mapOp;

        //AE控件的右键菜单
        public IToolbarMenu2 tocMenu;  //TOCControl的右键菜单

        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                //读取配置
                //。。。
                Configuration.ConfigPath = Application.StartupPath + "\\Config.ini";

                //暂时测试用图
                Configuration.MxdPath = Application.StartupPath + "\\map.mxd"; ;

                //初始化地图控制类对象
                mapOp = new MapOperator(this.mapControl);

                mapOp.LoadMxd(Configuration.MxdPath);

                //界面初始化
                mainUI = new MainFormUI(this);
                mainUI.InitMainForm();

                this.Cursor = Cursors.Default;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        
        }

        #region 界面风格部分代码
        /// <summary>
        /// 黑色风格
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuStyleBlack_Click(object sender, EventArgs e)
        {
            mainUI.ChangeStyleColor(this.menuStyleBlack);
        }

        private void menuStyleSilver_Click(object sender, EventArgs e)
        {
            mainUI.ChangeStyleColor(this.menuStyleSilver);
        }

        private void menuStyleBlue_Click(object sender, EventArgs e)
        {
            mainUI.ChangeStyleColor(this.menuStyleBlue);
        }

        private void menuStyleRed_Click(object sender, EventArgs e)
        {
            mainUI.ChangeStyleColor(this.menuStyleRed);
        }

        private void menuStyleGreen_Click(object sender, EventArgs e)
        {
            mainUI.ChangeStyleColor(this.menuStyleGreen);
        }

        private void menuStyleGolden_Click(object sender, EventArgs e)
        {
            mainUI.ChangeStyleColor(this.menuStyleGolden);
        }

        /// <summary>
        /// 自定义颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuStyleCustomize_Click(object sender, EventArgs e)
        {
            ColorDialog colorDlg = new ColorDialog();
            colorDlg.Color = mainUI.FormColor;
            colorDlg.AllowFullOpen = true;
            if (colorDlg.ShowDialog() == DialogResult.OK)
            {
                this.menuStyleCustomize.Tag = colorDlg.Color;
                mainUI.ChangeStyleColor(this.menuStyleCustomize);
            }
        }
        #endregion

        #region 显示状态栏文字
        private void ribbonBarMapNavigate_MouseEnter(object sender, EventArgs e)
        {
            BaseItem item = sender as BaseItem;

            if (item == null)
            {
                return;
            }

            this.labelStatus.Text = item.Tooltip;
        }

        private void ribbonBarEdit_Enter(object sender, EventArgs e)
        {
            BaseItem item = sender as BaseItem;

            if (item == null)
            {
                return;
            }

            this.labelStatus.Text = item.Tooltip;
        }

        private void ribbonBar_MouseLeave(object sender, EventArgs e)
        {
            this.labelStatus.Text = "";
        }
        #endregion

        #region 主菜单

        #region 地图浏览的菜单部分
        /// <summary>
        /// 刷新地图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuRefresh_Click(object sender, EventArgs e)
        {
            mapOp.RefreshMap();
        }

        /// <summary>
        /// 固定比例放大
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuZoomIn_Click(object sender, EventArgs e)
        {
            mapOp.FixedZoomIn();
            mainUI.SetExtentItem();
        }

        /// <summary>
        /// 固定比例缩小
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuZoomOut_Click(object sender, EventArgs e)
        {
            mapOp.FixedZoomOut();
            mainUI.SetExtentItem();
        }

        /// <summary>
        /// 显示全图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuFullExtent_Click(object sender, EventArgs e)
        {
            mapOp.FullExtent();
            mainUI.SetExtentItem();
        }

        /// <summary>
        /// 后退
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuBack_Click(object sender, EventArgs e)
        {
            mapOp.PrevExtent();
            mainUI.SetExtentItem();
        }

        /// <summary>
        /// 菜单：前进
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuForward_Click(object sender, EventArgs e)
        {
            mapOp.NextExtent();
            mainUI.SetExtentItem();
        }

        /// <summary>
        /// 菜单：移动工具
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuPan_Click(object sender, EventArgs e)
        {
            if (menuPan.Checked)
            {
                mapOp.SetToolNull();
                menuPan.Checked = false;
                toolCmdPan.Checked = false;
            }
            else
            {
                mapOp.SetToolMapPan();
                mainUI.UncheckMenu();
                mainUI.UncheckToolbar();
                menuPan.Checked = true;
                toolCmdPan.Checked = true;
            }
        }

        /// <summary>
        /// 菜单：选择工具
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuSelection_Click(object sender, EventArgs e)
        {
            if (menuSelection.Checked)
            {
                mapOp.SetToolNull();
                menuSelection.Checked = false;
                toolCmdSelect.Checked = false;
            }
            else
            {
                mapOp.SetToolSelectFeatures();
                mainUI.UncheckMenu();
                mainUI.UncheckToolbar();
                menuSelection.Checked = true;
                toolCmdSelect.Checked = true;
            }
        }

        /// <summary>
        /// 菜单：测距工具
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuMeasureDistance_Click(object sender, EventArgs e)
        {
            if (menuMeasureDistance.Checked)
            {
                mapOp.SetToolNull();
                menuMeasureDistance.Checked = false;
                toolCmdMeasure.Checked = false;
            }
            else
            {
                mapOp.SetToolMapMeasure();
                mainUI.UncheckToolbar();
                mainUI.UncheckMenu();
                menuMeasureDistance.Checked = true;
                toolCmdMeasure.Checked = true;
            }
        }

        /// <summary>
        /// 菜单：识别工具
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void menuIdentify_Click(object sender, EventArgs e)
        {
            if (menuIdentify.Checked)
            {
                mapOp.SetToolNull();
                menuIdentify.Checked = false;
                toolCmdIdentify.Checked = false;
            }
            else
            {
                mapOp.SetToolMapIdentify();
                mainUI.UncheckMenu();
                mainUI.UncheckToolbar();
                menuIdentify.Checked = true;
                toolCmdIdentify.Checked = true;
            }
        }



        #endregion

        #endregion

        #region 工具栏的点击
        /// <summary>
        /// 点击地图浏览部分的工具栏按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ribbonBarMapNavigate_ItemClick(object sender, EventArgs e)
        {
            ButtonItem item = sender as ButtonItem;

            if (item == null)
                return;

            //如按钮已经按下则设置工具为空
            if (item.Checked)
            {
                mapOp.SetToolNull();
                item.Checked = false;
                mainUI.UncheckMenu();
                return;
            }

            //判断按下的按钮
            switch (item.Name)
            {
                case "toolCmdPan":
                    mapOp.SetToolMapPan();
                    mainUI.UncheckMenu();
                    mainUI.UncheckToolbar();
                    item.Checked = true;
                    menuPan.Checked = true;
                    break;
                case "toolCmdZoomIn":
                    mapOp.SetToolMapZoomIn();
                    mainUI.UncheckMenu();
                    mainUI.UncheckToolbar();
                    item.Checked = true;
                    break;
                case "toolCmdZoomOut":
                    mapOp.SetToolMapZoomOut();
                    mainUI.UncheckMenu();
                    mainUI.UncheckToolbar();
                    item.Checked = true;
                    break;
                case "toolCmdSelect":
                    mapOp.SetToolSelectFeatures();
                    mainUI.UncheckMenu();
                    mainUI.UncheckToolbar();
                    item.Checked = true;
                    menuSelection.Checked = true;
                    break;
                case "toolCmdMeasure":
                    mapOp.SetToolMapMeasure();
                    mainUI.UncheckMenu();
                    mainUI.UncheckToolbar();
                    item.Checked = true;
                    menuMeasureDistance.Checked = true;
                    break;
                case "toolCmdIdentify":
                    mapOp.SetToolMapIdentify();
                    mainUI.UncheckMenu();
                    mainUI.UncheckToolbar();
                    item.Checked = true;
                    menuIdentify.Checked = true;
                    break;
                case "toolCmdFullExtent":
                    mapOp.FullExtent();
                    mainUI.SetExtentItem();
                    break;
                case "toolCmdNextExtent":
                    mapOp.NextExtent();
                    mainUI.SetExtentItem();
                    break;
                case "toolCmdPrevExtent":
                    mapOp.PrevExtent();
                    mainUI.SetExtentItem();
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region MapControl的响应事件
        private void mapControl_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
        {

        }

        private void mapControl_OnMouseMove(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseMoveEvent e)
        {
            string coordinate;
            coordinate = "鼠标坐标: X=" + e.mapX.ToString("N4") + ", Y=" + e.mapY.ToString("N4");
            labelCoordinate.Text = coordinate;
        }

        /// <summary>
        /// 鼠标松开后执行：
        /// 1.更改工具栏、菜单前进后退项目的状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mapControl_OnMouseUp(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseUpEvent e)
        {
            //设置主界面菜单、工具栏前进后退部分的状态
            mainUI.SetExtentItem();
        }

        /// <summary>
        /// 地图视图发生更改时（不是后，至少ExtentStack没有更新，真麻烦）执行
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void mapControl_OnExtentUpdated(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnExtentUpdatedEvent e)
        {
            mainUI.UpdateOverviewVisbleBounds();
        }
        #endregion

        private void menuAbout_Click(object sender, EventArgs e)
        {
            ILayer pLayer = mapOp.GetLayerByName("地名");
            MessageBox.Show(pLayer.Name);
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            //因为overviewForm是隐藏的，因此要重新将取消关闭赋值为假
            e.Cancel = false;

            //保存overviewForm的一些信息
            Ini.SetOverviewFormShow(menuOverview.Checked);
            Ini.SetOverviewFormLocation(mainUI.OverviewForm.Location);
            Ini.SetOverviewFormWidth(mainUI.OverviewForm.Width);
            Ini.SetOverviewFormHeight(mainUI.OverviewForm.Height);
        }

        private void menuOverview_Click(object sender, EventArgs e)
        {
            menuOverview.Checked = !menuOverview.Checked;

            if (menuOverview.Checked)
            {
                mainUI.ShowOverview();
            }
            else
            {
                mainUI.HideOverview();
            }
        }

        private void tocControl_OnMouseDown(object sender, ESRI.ArcGIS.Controls.ITOCControlEvents_OnMouseDownEvent e)
        {

        }

        /// <summary>
        /// 响应在TOCControl中松开鼠标的事件，用于弹出右键菜单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tocControl_OnMouseUp(object sender, ESRI.ArcGIS.Controls.ITOCControlEvents_OnMouseUpEvent e)
        {
            if (e.button != 2)
                return;

            //检查鼠标点击的项目
            esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
            IBasicMap map = null;
            object other = null;
            object index = null;
            ILayer selectLayer = null;

            tocControl.HitTest(e.x, e.y, ref item, ref map, ref selectLayer, ref other, ref index);

            //移除原有的右键菜单
            tocMenu.RemoveAll();

            //判断并添加菜单
            if (item == esriTOCControlItem.esriTOCControlItemLayer)
            {
                if(selectLayer is IFeatureLayer)
                {
                    tocMenu.AddItem(new CmdLayerSetting((IFeatureLayer)selectLayer), 0, 0, false, ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconAndText);
                }
            }

            //弹出菜单
            tocMenu.PopupMenu(e.x, e.y, tocControl.hWnd);
        }
 
    }
}